#include "db_dat.h"
#include "re_chck.h"
#include "common.h"

//0x0100
void term_register_trans(TERM_REG_FM *tmReg_fm)
{
	INT32 ix;
	tmReg_fm->province = g_vehicleArgs.ProvinceId;
	tmReg_fm->city = g_vehicleArgs.CityId;

	memcpy(tmReg_fm->producer, g_installArgs.mfrId, sizeof(g_installArgs.mfrId));	

	memcpy(tmReg_fm->type1, g_installArgs.tmType, sizeof(g_installArgs.tmType));

	memcpy(tmReg_fm->no1, g_installArgs.tmlId, 7);

	tmReg_fm->color = g_vehicleArgs.PlateColor;

	memset(tmReg_fm->license, 0, strlen(tmReg_fm->license));
	memcpy(tmReg_fm->license, g_vehicleArgs.PlateNum, sizeof(g_vehicleArgs.PlateNum));

	E(" provId: %d. city: %d. mfrId: %s. tmType: %s. tmlId: %s. color: %d. num: %s",	\
		tmReg_fm->province, tmReg_fm->city, tmReg_fm->producer, tmReg_fm->type1, tmReg_fm->no1, tmReg_fm->color, tmReg_fm->license);		
/*	for(ix = 0; ix < 7; ix++){
		fprintf(stdout, "%02x ", g_installArgs.tmlId[ix]);
	}
	putchar('\n');
	for(ix = 0; ix < 7; ix++){
		fprintf(stdout, "%02x ", tmReg_fm->no1[ix]);
	}
*/
}

//0x0200
void posInfo_report_trans(POS_INFO *pPos, POS_INFO_FM *pPos_fm)
{
	float lat, lng, speed;
	float mileage, oilmass;
	INT32 r;

	//base positon.
	lat = pPos->base.gps.latitude/(pow(10, 6));
	lng = pPos->base.gps.longitude/(pow(10, 6));
	speed = pPos->base.gps.speed/10;

	pPos_fm->base_fm.altitude = pPos->base.gps.height;
	pPos_fm->base_fm.direction = pPos->base.gps.course;
	pPos_fm->base_fm.lat = lat;
	pPos_fm->base_fm.lng = lng;
	pPos_fm->base_fm.speed = speed;
	pPos_fm->base_fm.status = pPos->base.status;

/*	E(" lat: %f, lng: %f, speed: %f, altitude: %d, direction: %d, status: %d", lat, lng, speed, pPos_fm->base_fm.altitude,	\
		pPos_fm->base_fm.direction, pPos_fm->base_fm.status);
*/
	r = time_convert(pPos->base.gps.tm, pPos_fm->base_fm.time);
	if(r == -1){
		E(" Time_convert error.");
		exit(1);
	}
	//E(" time: %s", pPos_fm->base_fm.time);	
	pPos_fm->base_fm.warning = pPos->base.alarmFlag;
	//E(" warning: %d", pPos_fm->base_fm.warning);

	//add positon.
	mileage = pPos->add.mileage/10;
	oilmass = pPos->add.fuel/10;
	speed = pPos->add.speed/10;

	pPos_fm->add_fm.area_id = pPos->add.areaAdd.id;
	pPos_fm->add_fm.areatype = pPos->add.areaAdd.type;
	pPos_fm->add_fm.direction = pPos->add.areaAdd.drc;
	pPos_fm->add_fm.mileage = mileage;
	pPos_fm->add_fm.oilmass = oilmass;
	pPos_fm->add_fm.speed = speed;
	pPos_fm->add_fm.positiontype = pPos->add.overSpdAdd.type;
	pPos_fm->add_fm.result = pPos->add.drvTm.result;
	pPos_fm->add_fm.section_id = pPos->add.drvTm.id;
	pPos_fm->add_fm.sectiontime = pPos->add.drvTm.tm;
}

//0x0201
void posInfo_search_trans(POSINFO_SERCHRPY *pPos_sech, POSINFO_SERCHRPY_FM *pPos_sechFm)
{
	INT32 r;
	float lat, lng, speed;
//	float mileage, oilmass;

	lat = pPos_sech->posInfo.base.gps.latitude/(pow(10, 6));
	lng = pPos_sech->posInfo.base.gps.longitude/(pow(10, 6));
	speed = pPos_sech->posInfo.base.gps.speed/10;

	pPos_sechFm->altitude = pPos_sech->posInfo.base.gps.height;
	pPos_sechFm->direction = pPos_sech->posInfo.base.gps.course;
	pPos_sechFm->lat = lat;
	pPos_sechFm->lng = lng;
	pPos_sechFm->speed = speed;
	pPos_sechFm->status = pPos_sech->posInfo.base.status;

	r = time_convert(pPos_sech->posInfo.base.gps.tm, pPos_sechFm->time);
	if(r == -1){
		E(" Time_convert error.");
		exit(1);
	}	
	pPos_sechFm->warning = pPos_sech->posInfo.base.alarmFlag;

}
//0x0301 ok!

//0x0302 ok!


//0x0500 ok!

//0x0700 ok!

//0x0701 ok!

//0x0702
void drvInfo_upload_trans(DRIVER_INFO *drvInfo, DRIVER_INFO_FM *dvfInfoFm)
{
	sprintf(dvfInfoFm->idnumber, "%s", drvInfo->IDcard);

	sprintf(dvfInfoFm->issue, "%s", drvInfo->orgName);
	sprintf(dvfInfoFm->license, "%s", drvInfo->drvNum);
	sprintf(dvfInfoFm->name, "%s", drvInfo->name);

}

#if 1
//0x0800
void media_event_trans(MEDIA_EVENT_UP *medEvent, MEDIA_EVENT_FM *medEventFm)
{
	medEventFm->channel = medEvent->channelID;

	/* data id.*/
	sprintf(medEventFm->dataid, "%d", medEvent->mediaDatID);

	switch(medEvent->eventCode){
		case 0x0:
			memcpy(medEventFm->event, "平台下发指令", sizeof("平台下发指令")); break;
		case 0x01:
			memcpy(medEventFm->event, "定时动作", sizeof("定时动作")); break;
		case 0x02:
			memcpy(medEventFm->event, "抢劫报警触发", sizeof("抢劫报警触发")); break;
		case 0x03:
			memcpy(medEventFm->event, "碰撞侧翻报警触发", sizeof("碰撞侧翻报警触发")); break;
		default:
			break;
	}


	switch(medEvent->mediaCode){
		case 0x0:
			memcpy(medEventFm->format, "JPEG", sizeof("JPEG")); break;
		case 0x01:
			memcpy(medEventFm->format, "TIF", sizeof("TIF")); break;
		case 0x02:
			memcpy(medEventFm->format, "MP3", sizeof("MP3")); break;
		case 0x03:
			memcpy(medEventFm->format, "WAV", sizeof("WAV")); break;
		case 0x04:
			memcpy(medEventFm->format, "WMV", sizeof("WMV")); break;
		default:
			break;
	}

	/* media type.*/
	switch(medEvent->mediaType){
		case 0x0: 
			memcpy(medEventFm->type, "图像", sizeof("图像")); break;
		case 0x01:
			memcpy(medEventFm->type, "音频", sizeof("音频")); break;
		case 0x02:
			memcpy(medEventFm->type, "视频", sizeof("视频")); break;
		default:
			break;
	}	
}
#endif
#if 1
//0x0801
void media_data_trans(MEDIA_DAT_UP *medDat, MEDIA_DAT_FM *medDatFm)
{
	medDatFm->pos_id = medDat->channelID;

	/* data id.*/
	sprintf(medDatFm->dataid, "%d", medDat->mediaDatID);

	switch(medDat->eventCode){
		case 0x0:
			memcpy(medDatFm->event, "平台下发指令", sizeof("平台下发指令")); break;
		case 0x01:
			memcpy(medDatFm->event, "定时动作", sizeof("定时动作")); break;
		case 0x02:
			memcpy(medDatFm->event, "抢劫报警触发", sizeof("抢劫报警触发")); break;
		case 0x03:
			memcpy(medDatFm->event, "碰撞侧翻报警触发", sizeof("碰撞侧翻报警触发")); break;
		default:
			break;
	}


	switch(medDat->mediaCode){
		case 0x0:
			memcpy(medDatFm->format, "JPEG", sizeof("JPEG")); break;
		case 0x01:
			memcpy(medDatFm->format, "TIF", sizeof("TIF")); break;
		case 0x02:
			memcpy(medDatFm->format, "MP3", sizeof("MP3")); break;
		case 0x03:
			memcpy(medDatFm->format, "WAV", sizeof("WAV")); break;
		case 0x04:
			memcpy(medDatFm->format, "WMV", sizeof("WMV")); break;
		default:
			break;
	}

	/* media type.*/
	switch(medDat->mediaType){
		case 0x0: 
			memcpy(medDatFm->type, "图像", sizeof("图像")); break;
		case 0x01:
			memcpy(medDatFm->type, "音频", sizeof("音频")); break;
		case 0x02:
			memcpy(medDatFm->type, "视频", sizeof("视频")); break;
		default:
			break;
	}	

	memcpy(medDatFm->content, medDat->mediaPkg, sizeof(medDatFm->content));	
}
#endif
